package com.wushi.lenovo.asia5b.app_ui.h5;

import android.os.Bundle;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.mvp.base.model.ResultModel;
import com.mvp.common.GlobalValue;
import com.mvp.utils.JsonUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_event.OrderPayEvent;
import com.wushi.lenovo.asia5b.app_event.TopupEvent;
import com.wushi.lenovo.asia5b.app_model.credit.bean.PayInsFpxBean;
import com.wushi.lenovo.asia5b.app_model.credit.bean.ThirdTopupType;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupInfoBean;
import com.wushi.lenovo.asia5b.app_model.h5.JsCall;
import com.wushi.lenovo.asia5b.app_widget.ScorllWebView;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.ResponseBody;
import timber.log.Timber;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/2/19.
 * 第三方充值/订单付款页面
 */
public class TopupThirdActivity extends H5Activity {
    @BindView(R.id.pb_topupthid)
    ContentLoadingProgressBar progress;
    @BindView(R.id.ll_topupthird_vessel)
    LinearLayout ll_topupthird_vessel;
    protected WebView webView;
    Map<String, Object> map;
    ThirdTopupType thirdTopupType;
    public TopupInfoBean topupInfoBean = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_topupthird;
    }

    @Override
    protected WebView getWebView() {
        return webView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        map = (Map<String, Object>) getIntent().getSerializableExtra("map");
        thirdTopupType = (ThirdTopupType) getIntent().getSerializableExtra(CommonConstant.TOPUP_THIRDTOPUPTYPE);
        if (getIntent().hasExtra(CommonConstant.TopupInfoBean)) {
            topupInfoBean = (TopupInfoBean) getIntent().getSerializableExtra(CommonConstant.TopupInfoBean);
        }
        Timber.d("--->>>>>>>>---map:" + map);
        Timber.d("--->>>>>>>>---thirdTopupType:" + thirdTopupType);
        initView();
        initEvent();
        initData();
    }

    @Override
    protected void initView() {
        if (GlobalValue.isDebug && false) {
            Observable.timer(3, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> {
                        ResultModel resultModel = new ResultModel();
                        resultModel.setResult(1);
                        resultModel.setMsg("test");
                        OrderPayEvent orderPayEvent = new OrderPayEvent(resultModel);
                        orderPayEvent.topupInfoBean=topupInfoBean;
                        EventBus.getDefault().postSticky(orderPayEvent);
                        finish();
                    });
        }

        //使用new方式这是为了解决当(settings.setJavaScriptEnabled(true))之后会出现TextToSpeech的内存泄漏
        webView = new ScorllWebView(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        webView.setLayoutParams(layoutParams);
        ll_topupthird_vessel.addView(webView);
        setting(webView);
        setWebViewChormeClient(webView, progress);


    }

    /*
    window.Android.HtmlcallJava2(JSON.stringify({ 'result': '1', 'msg': '指定的帐户无效/不存在', 'data': [] }));
     */
    private void parseParams(String param) {

        ResultModel resultModel = JsonUtil.INSTANCE.jsonToAny(param, ResultModel.class);
        if (resultModel == null) {
            resultModel = new ResultModel();
            resultModel.setMsg("error!");
        }
        if (thirdTopupType == ThirdTopupType.thirdtopup_by_fpx ||
                thirdTopupType == ThirdTopupType.thirdtopup_by_creditcard) {//充值
            EventBus.getDefault().post(new TopupEvent(resultModel));
        } else {//订单付款
            //todo 订单付款
            OrderPayEvent orderPayEvent = new OrderPayEvent(resultModel);
            orderPayEvent.topupInfoBean=topupInfoBean;
            EventBus.getDefault().postSticky(orderPayEvent);
        }
        finish();


    }

    @Override
    protected void initData() {
        getPresenter().thirdTopup(map, thirdTopupType);
    }

    @Override
    protected void initEvent() {
        jsCall = new JsCall() {
            @Override
            public void callbackParam(String param) {
                parseParams(param);
            }
        };
        webView.addJavascriptInterface(jsCall, H5_JSCALL_NAME);
    }

    @Override
    public void thirdTopupResult(ResponseBody responseBody) {
        String string = null;
        try {
            string = responseBody.string();
            if (thirdTopupType == ThirdTopupType.thirdtopup_from_orderpayment_ins_by_fpx ||
                    thirdTopupType == ThirdTopupType.thirdtopup_from_orderpayment_ins_by_creditcard) {
                PayInsFpxBean payInsFpxBean = JsonUtil.INSTANCE.jsonToAny(string, PayInsFpxBean.class);
                if (payInsFpxBean != null) {
                    string = payInsFpxBean.getHtml();
                }
            } else {
            }
//            if (GlobalValue.isDebug) {
//                String s = "type=\"hidden\"";
//                string = string.replaceAll(s, "");
//            }
            d("--->>>>>>>>---string:" + string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(string)) {
            webView.loadData(string, "text/html", "utf-8");
        }
    }

}
